layui.use(['jquery', 'table', 'util', 'form', 'laydate', 'layer'], function() {
	var $ = layui.jquery;
	var table = layui.table;
	var util = layui.util;
	var form = layui.form;
	var laydate = layui.laydate;
	var layer = layui.layer;

	table.render({
		elem: '#table',
		url: 'tableData',
		page: true,
		skin: 'line',
		request: {
			pageName: 'current',
			limitName: 'size'
		},
		cols: [[
			{field: 'userId', title: '操作用户', templet: function(d) {
				return '<a href="../user/list?username=' + d.user.username + '">' + d.user.name + '</a>';
			}},
			{field: 'host', title: '操作主机'},
			{field: 'uri', title: '访问路径'},
			{field: 'params', title: '访问参数', templet: function(d) {
				return '<a class="layui-btn layui-btn-xs" lay-event="params">查看</a>';
			}},
			{field: 'name', title: '异常类名称'},
			{title: '堆栈追踪', templet: function(d) {
				return '<a class="layui-btn layui-btn-xs" lay-event="stackTrace">查看</a>';
			}},
			{field: 'addTime', title: '发生时间', minWidth: 180, sort: true, templet: function(d) {
				return util.toDateString(d.addTime);
			}}
		]],
		autoSort: false,
		initSort: {
			field: 'addTime',
			type: 'desc'
		},
		where: {
			orderByField: 'addTime',
			isAsc: false
		}
	});
	table.on('tool', function(obj) {
		var data = obj.data;

		switch (obj.event) {
		case 'params':
			layer.open({
				type: 1,
				title: '访问参数',
				shadeClose: true,
				area: ['800px', '600px'],
				content: '<pre class="margin-15">' + JSON.stringify(data.params, null, 4) + '</pre>'
			});
			break;
		case 'stackTrace':
			layer.open({
				type: 1,
				title: '堆栈追踪',
				shadeClose: true,
				area: ['800px', '600px'],
				content: '<pre class="margin-15">' + data.stackTrace + '</pre>'
			});
			break;
		default:
			break;
		}
	});
	table.on('sort', function(obj) {
		table.reload('table', {
			initSort: obj,
			where: {
				orderByField: obj.field,
				isAsc: obj.type ? obj.type == 'asc' : null
			}
		})
	});
	form.on('submit(search)', function(data) {
		table.reload('table', {
			where: data.field,
			page: {
				curr: 1
			}
		});
	});
	laydate.render({
		elem: '[name="addTime"]',
		type: 'datetime',
		range: true
	});
});